// pages/work/detail/selPerson.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    id:'9354ffa5db3d4798b3e841323438e37a',
    selPerson:'',
    hidden:false,
    treeData: [],
    dept:{"id":null,"text":null},
    loadingHidden:false,
    windowHeight:0

  },
  //获取列表数据
  getDataList(_this){
    _this.setData({
      loadingHidden: false
    });
    let deptId = _this.data.dept.id;
    var url = getApp().data.app_server + "/weixin/org/selectUserListByDept?token=" + app.data.token + "&deptId=" + deptId;
    if (deptId == null || deptId == ''){
      url = getApp().data.app_server + "/weixin/org/selectUserListByDept?token=" + app.data.token;
    }
    wx.request({
      url: url,
      method: 'get',
      header: { 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8' },
      success: function (res) {
        console.log(res);
        _this.setData({
          list: res.data,
          loadingHidden:true
        })
      }
    })
  },
  radioChange(e) {
    console.log(e.detail.value);
    this.setData({
      selPerson: e.detail.value
    })
  },
  //确认操作
  sub(e){
    var arr = this.data.selPerson.split('-');
    app.data.selPerson.id = arr[0];
    app.data.selPerson.userName = arr[1];
    console.log(app.data.selPerson);
    wx.navigateBack({
      delta: 1
    })
  },
  closeForm(e){
    wx.navigateBack({
      delta: 1
    })
  },
  //事件处理函数
  tapItem: function (e) {
    var hidden = !this.data.hidden;
    console.log('index接收到的itemid: ' + e.detail.itemid);
    this.setData({
      ['dept.id']: e.detail.itemid,
      ['dept.text']: e.detail.itemval,
      hidden: hidden
    })
    this.getDataList(this);
  },
  showTab(e) {
    var hidden = !this.data.hidden;
    this.setData({
      hidden: hidden
    })
  },
  //获取组织数据
  getDeptData(parentForlderId){
    let _this = this;
    wx.request({
      url: getApp().data.app_server + "/weixin/org/selectDeptTreeData?token=" + app.data.token,
      method: 'get',
      header: { 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8' },
      success: function (res) {
        if (res.data != null && res.data.length > 0){
          res.data.unshift({ id: '', folderName:'所有'});
          _this.setData({
            treeData: res.data,
            ['dept.id']:res.data[0].id,
            ['dept.text']: res.data[0].folderName
          })
          _this.getDataList(_this);//默认加载用户数据
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(this.data.dept);
    var parentForlderId = 'root';
    this.getDeptData(parentForlderId);
    //this.getDataList(this);
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        console.info(res.windowHeight);
        that.setData({
          windowHeight: res.windowHeight - 100
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})